package com.hzjh.h5mall.ui.forgot

import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.R
import com.hzjh.base.style.view.edittext.SplitEditText.OnTextInputListener
import com.hzjh.base.utils.function.countDown
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.databinding.ActivityForgotStep2Binding
import com.hzjh.h5mall.model.forgot.ForgotVM

@Route(path = RouteUrl.FORGOT_STEP2_ACTIVITY)
class ForgotStep2Activity : JHBaseActivity<ActivityForgotStep2Binding,ForgotVM>() {
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_EMAIL)
    var email = ""

    private var ifCanSendMsg=true
    override fun initData() {
        //自动发送验证码
        viewModel.reqForgetEmailCaptcha(email)
    }

    override fun initView() {
        viewBinding.inputCode.setOnTextInputListener(object :OnTextInputListener {
            override fun onTextInputChanged(text: String, length: Int) {

            }

            override fun onTextInputCompleted(text: String) {
                //请求接口
                navigationTo(RouteUrl.FORGOT_STEP3_ACTIVITY){
                    withString(RouteUrl.PARAMS_EMAIL,email)
                    withString(RouteUrl.PARAMS_CAPTCHA,text)
                }
            }
        })

        viewBinding.tvResendMsg.setOnClickListener {
            if(ifCanSendMsg){
                //再次发送短信验证码
                viewModel.reqForgetEmailCaptcha(email)
                countDown(29,
                    start = {
                        ifCanSendMsg=false
                        viewBinding.tvResendMsg.setTextColor(CommUtils.getColor(R.color.coreBlack4))
                        viewBinding.tvResendMsg.text = CommUtils.getString(com.hzjh.base.ext.R.string.jh_resend_time, 30)
                    },
                    end = {
                        ifCanSendMsg=true
                        viewBinding.tvResendMsg.setTextColor(CommUtils.getColor(R.color.coreRed))
                        viewBinding.tvResendMsg.text = CommUtils.getString(com.hzjh.base.ext.R.string.jh_resend)
                    },
                    next = {
                        viewBinding.tvResendMsg.text = CommUtils.getString(com.hzjh.base.ext.R.string.jh_resend_time, it)
                    }
                )
            }
        }
    }

    override fun initViewBinding(): ActivityForgotStep2Binding {
        return ActivityForgotStep2Binding.inflate(layoutInflater)
    }
}